      <h1 class="entry-title entry-title-single" itemprop="headline">SAP维护视图(2)-表维护视图事件处理</h1>

    <div class="entry-content entry-content-single entry-content-aside" itemprop="text">
      <p><strong>1. 前言</strong></p>
<p>上篇文章简单介绍了表<span class='wp_keywordlink_affiliate'><a href="http://www.sapjx.com/tag/table-maintenance-view" title="查看维护视图中的全部文章" target="_blank">维护视图</a></span>的生成，也提及到视图事件的扩展，这里主要讲述也是关于视图的事件处理。</p>
<p>当为表生成了<span class='wp_keywordlink_affiliate'><a href="http://www.sapjx.com/tag/table-maintenance-view" title="查看维护视图中的全部文章" target="_blank">维护视图</a></span>后，在用户操作体验时，总是会出现一些很莫名的需求；</p>
<p>然而很多时候明明在<span class='wp_keywordlink_affiliate'><a href="http://www.sapjx.com/tag/table-maintenance-view" title="查看维护视图中的全部文章" target="_blank">维护视图</a></span>上能做到，但往往却退而求其次的额外开发新的程序去维护数据。</p>
<p>不仅浪费时间又浪费资源，会出现这种尴尬的情况，主要还是对表维护生成器的不熟悉导致的。</p>
<p><span id="more-642"></span>&nbsp;</p>
<p>以下是笔者以前遇到过的一些常用的客户需求：</p>
<p>需求一，输入客户编码后自动带出客户名称、输出物料自动带出物料描述 …… (这个上篇文章提过，也是最经常出现的需求)</p>
<p>需求二，输入日期后，根据逻辑自动算出截止日期</p>
<p>需求三，在维护视图中批量导入/导出数据</p>
<p>需求四，进入维护视图前，对数据进行条件筛选</p>
<p>需求五，针对不同操作人员，显示不同的数据</p>
<p>需求六，……</p>
<p>类似的需求还有很多，这里就不一一列举，但归根结底，很多需求都可以通过维护视图事件来解决。</p>
<p>&nbsp;</p>
<p><strong>2. 维护视图相关<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="函数" target="_blank">函数</a></span></strong></p>
<p>在讲述维护视图的事件之前，先看下表维护生成器几个重要的<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="Function" target="_blank">Function</a></span> Module：</p>
<p><strong>01). VIEW_MAINTENANCE_CALL</strong> – 调用维护视图<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="函数" target="_blank">函数</a></span></p>
<p><strong>02). VIEW_GET_DDIC_INFO</strong> &#8211; 获取数据字典信息</p>
<p><strong>03). VIEW_AUTHORITY_CHECK</strong> – 检查权限</p>
<p><strong>04). VIEW_MAINTENANCE</strong> – 调用生成的扩展维护视图</p>
<p><strong>05). VIEW_ENQUEUE</strong> – 视图加锁/解锁</p>
<p>其中，VIEW_MAINTENANCE_CALL<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="函数" target="_blank">函数</a></span>是维护视图的入口函数，包含几个部分内容：</p>
<p>通过VIEW_GET_DDIC_INFO函数抓取数据字典中的结构信息来填充视图框架结构（即：Table Control 数据填充）；</p>
<p>通过VIEW_AUTHORITY_CHECK函数对操作用户进行授权检查；</p>
<p>通过VIEW_MAINTENANCE 函数调用生成的扩展维护视图，即动态调用生成维护视图的函数组中以TABLEFRAME_开头的函数；</p>
<p>在操作过程中通过 VIEW_ENQUEUE 函数对操作视图加锁，操作结束时，进行解锁。</p>
<p>&nbsp;</p>
<p><strong>3.事件处理</strong></p>
<p>维护视图中的事件有38个，其中附加事件28个，替换事件10个，分别是：</p>
<p><b>附加事件(Additional events):</b></p>
<p><span class='wp_keywordlink_affiliate'><a href="http://www.sapjx.com/tag/event" title="查看Event中的全部文章" target="_blank">Event</a></span> 01 before saving the data in the data</p>
<p><span class='wp_keywordlink_affiliate'><a href="http://www.sapjx.com/tag/event" title="查看Event中的全部文章" target="_blank">Event</a></span> 02 after saving the data in the database</p>
<p>Event 03 before deleting the display data</p>
<p>Event 04 after deletion of the display data</p>
<p>Event 05 when inserting a new entry</p>
<p>Event 06 after complete execution of <span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="Function" target="_blank">Function</a></span> &#8216;Get Original&#8217;</p>
<p>Event 07 before correcting the contents of a selected field</p>
<p>Event 08 after correcting the contents of a specified field</p>
<p>Event 09 after &#8216;Get Original&#8217; of a single entry</p>
<p>Event 10 after creating change request header entries</p>
<p>Event 11 after changing a key entry in the change request</p>
<p>Event 12 after changing the key entries in the change request</p>
<p>Event 13 end of processing (leaving the main <span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="Function" target="_blank">Function</a></span> module)</p>
<p>Event 14 after lock/unlock in the main <span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="Function Module" target="_blank">Function Module</a></span></p>
<p>Event 15 before retrieving deleted entries</p>
<p>Event 16 after recovering deleted entries</p>
<p>Event 17 before printing entries</p>
<p>Event 18 after checking whether the data have been changed</p>
<p>Event 19 after the initialization of global variables, field symbols, and so on</p>
<p>Event 20 after input in date subscreen (time-dep.tables/views)</p>
<p>Event 21 fill hidden fields</p>
<p>Event 22 Go TO long text maintenance for other languages</p>
<p>Event 23 before calling the address maintenance screen</p>
<p>Event 24 After restricting an entry (time-dep. tab./views)</p>
<p>Event 25 at start of maintenance dialog</p>
<p>Event 26 before displayed data is output in a list</p>
<p>Event 27 after filling a GUID field</p>
<p>Event 28 after entering a date restriction for time-dep. views</p>
<p>&nbsp;</p>
<p><b>替换事件(Replacement events):</b></p>
<p>Event AA instead of the standard routine for reading data</p>
<p>Event AB instead of the standard database change routine</p>
<p>Event AC instead of standard routine for &#8216;Get Original&#8217;</p>
<p>Event AD (Instead of standard RO field read routine)</p>
<p>Event AE instead of the standard positioning code</p>
<p>Event AF Instead of reading in texts in other languages</p>
<p>Event AG Instead of &#8216;Get original&#8217; for texts in other languages</p>
<p>Event AH instead of database changes for texts in other languages</p>
<p>Event ST Name of the host program for the GUI menu</p>
<p>Event AI Internal use only</p>
<p>&nbsp;</p>
<p><em>具体每个事件的操作可以查看相关说明文档：<br />
<a href="http://help.sap.com/saphelp_nw73/helpdata/en/4d/d57dc3310c0f29e10000000a15822b/content.htm" target="_blank">http://help.sap.com/saphelp_nw73/helpdata/en/4d/d57dc3310c0f29e10000000a15822b/content.htm</a><br />
</em></p>
<p>&nbsp;</p>
<p>当为维护视图添加事件后，可以在透明表<b>TVMIF</b>中看到视图所定义的事件名称。</p>
<p>事件标识，也可以在结构<b>VIMDESC</b>中找到相应的对应关系；</p>
<p>在标准程序中，位于 <strong>VIEW_GET_DDIC_INFO</strong> 函数，其中的对应关系如下所示：</p>
<pre class="brush: abap;">
* 1. Get events
***********************************************************************
  LOOP AT tvimf_tab.
    CASE tvimf_tab-event.
      WHEN '01'. MOVE tvimf_tab-formname TO x_header-frm_bf_sav.
      WHEN '02'. MOVE tvimf_tab-formname TO x_header-frm_af_sav.
      WHEN '03'. MOVE tvimf_tab-formname TO x_header-frm_bf_del.
      WHEN '04'. MOVE tvimf_tab-formname TO x_header-frm_af_del.
      WHEN '05'. MOVE tvimf_tab-formname TO x_header-frm_on_new.
      WHEN '06'. MOVE tvimf_tab-formname TO x_header-frm_af_org.
      WHEN '07'. MOVE tvimf_tab-formname TO x_header-frm_bf_rpl.
      WHEN '08'. MOVE tvimf_tab-formname TO x_header-frm_af_rpl.
      WHEN '09'. MOVE tvimf_tab-formname TO x_header-frm_on_org.
      WHEN '10'. MOVE tvimf_tab-formname TO x_header-frm_e071.
      WHEN '11'. MOVE tvimf_tab-formname TO x_header-frm_e071ks.
      WHEN '12'. MOVE tvimf_tab-formname TO x_header-frm_e071ka.
      WHEN '13'. MOVE tvimf_tab-formname TO x_header-frm_bf_end.
      WHEN '14'. MOVE tvimf_tab-formname TO x_header-frm_af_enq.
      WHEN '15'. MOVE tvimf_tab-formname TO x_header-frm_bf_udl.
      WHEN '16'. MOVE tvimf_tab-formname TO x_header-frm_af_udl.
      WHEN '17'. MOVE tvimf_tab-formname TO x_header-frm_bf_prn.
      WHEN '18'. MOVE tvimf_tab-formname TO x_header-frm_af_chk.
      WHEN '19'. MOVE tvimf_tab-formname TO x_header-frm_af_ini.
      WHEN '20'. MOVE tvimf_tab-formname TO x_header-frm_in_dss.
      WHEN '21'. MOVE tvimf_tab-formname TO x_header-frm_h_flds.
      WHEN '22'. MOVE tvimf_tab-formname TO x_header-frm_tltext.
      WHEN '23'. MOVE tvimf_tab-formname TO x_header-frm_bf_adr.
      WHEN '24'. MOVE tvimf_tab-formname TO x_header-frm_af_dlm.
      WHEN '25'. MOVE tvimf_tab-formname TO x_header-frm_on_aut.
      WHEN '26'. MOVE tvimf_tab-formname TO x_header-frm_bf_alv. "UF
      WHEN '27'. MOVE tvimf_tab-formname TO x_header-frm_af_uid. "UF
      WHEN 'AA'. MOVE tvimf_tab-formname TO x_header-frm_rp_get.
      WHEN 'AB'. MOVE tvimf_tab-formname TO x_header-frm_rp_upd.
      WHEN 'AC'. MOVE tvimf_tab-formname TO x_header-frm_rp_org.
      WHEN 'AD'. MOVE tvimf_tab-formname TO x_header-frm_rp_cpl.
      WHEN 'AE'. MOVE tvimf_tab-formname TO x_header-frm_rp_pos.
      WHEN 'AF'. MOVE tvimf_tab-formname TO x_header-frm_tl_get. "SW
      WHEN 'AG'. MOVE tvimf_tab-formname TO x_header-frm_tl_org. "SW
      WHEN 'AH'. MOVE tvimf_tab-formname TO x_header-frm_tl_upd. "SW
      WHEN 'AI'. MOVE tvimf_tab-formname TO x_header-frm_bf_alv. "UF
      WHEN 'ST'. MOVE tvimf_tab-formname TO x_header-gui_prog.
    ENDCASE.
  ENDLOOP.
</pre>
<p>&nbsp;</p>
<p>便于在编辑程序时，找寻哪个事件用于什么位置，引用上下文内容等好处。</p>
<p>例如，如果定义了<b>AB</b> (Instead of the standard database change routine) 事件，</p>
<p>在程序中，就能直接找到调用增加的事件：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_01_Event_AB.jpg" alt="table_maintenance_view_02_01_Event_AB" width="568" height="214" class="alignnone size-full wp-image-655" /></p>
<p>&nbsp;</p>
<p><strong>3. 添加维护视图事件</strong></p>
<p>添加维护视图事件的操作比较简单，通过生成器生成视图后，选择菜单：Environment -&gt; Modification -&gt; Events</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_02_Event_Menu.jpg" alt="table_maintenance_view_02_02_Event_Menu" width="666" height="165" class="alignnone size-full wp-image-656" /></p>
<p>进入事件界面，在工具栏上点击  <b>添加新条目</b>（New Entries），即可添加事件：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_03_Add_Event.jpg" alt="table_maintenance_view_02_03_Add_Event" width="380" height="277" class="alignnone size-full wp-image-657" /></p>
<p>在第一个栏位上选择事件，然后在FORM Routine中输入自定义事件的Subroutine名称。</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_03_Select_Event.jpg" alt="table_maintenance_view_02_03_Select_Event" width="557" height="455" class="alignnone size-full wp-image-658" /></p>
<p>这里需要说明的是，添加的事件程序，是通过执行Form操作的，因此在编辑事件程序时，可以包含在Include文件中。</p>
<p>添加成功后，再点击编辑，把其保存到新的Include文件，即可对该事件添加程序：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_04_Event_Editor.jpg" alt="table_maintenance_view_02_04_Event_Editor" width="378" height="164" class="alignnone size-full wp-image-659" /></p>
<p>&nbsp;&nbsp;</p>
<p><strong>4. 维护视图事件处理示例</strong></p>
<p>步骤一，新建Add-On表并输入各字段内容：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_05_Demo_Create_Add_On.jpg" alt="table_maintenance_view_02_05_Demo_Create_Add_On" width="735" height="377" class="alignnone size-full wp-image-660" /></p>
<p>步骤二，维护技术参数，保存数据表并激活；</p>
<p>步骤三，生成维护视图：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_06_Demo_Table_Maintenance_Generation_Environment.jpg" alt="table_maintenance_view_02_06_Demo_Table_Maintenance_Generation_Environment" width="590" height="484" class="alignnone size-full wp-image-667" /></p>
<p>步骤四，添加事件并保存：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_07_Demo_Add_Event_05.jpg" alt="table_maintenance_view_02_07_Demo_Add_Event_05" width="368" height="177" class="alignnone size-full wp-image-661" /></p>
<p>步骤五，点击编辑器编辑事件：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_08_Demo_Event_05.jpg" alt="table_maintenance_view_02_08_Demo_Event_05" width="661" height="585" class="alignnone size-full wp-image-663" /></p>
<p>输入客户编号、物料编码时，自动带出客户名称与物料描述。</p>
<p>&nbsp;</p>
<p>输出效果：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_09_Demo_Event_05_show1.jpg" alt="table_maintenance_view_02_09_Demo_Event_05_show1" width="681" height="142" class="alignnone size-full wp-image-664" /></p>
<p>回车后，自动带出描述：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_10_Demo_Event_05_show2.jpg" alt="table_maintenance_view_02_10_Demo_Event_05_show2" width="678" height="170" class="alignnone size-full wp-image-665" /></p>
<p>&nbsp;</p>
<p>步骤六，添加事件21</p>
<p>由于事件05是添加新条目时触发的事件，当在更新数据时，并不会触发事件；</p>
<p>因此，需要添加事件21，这样在栏位上输入值时，可以对其栏位进行校验或者取数：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_11_Demo_Add_Event_21.jpg" alt="table_maintenance_view_02_11_Demo_Add_Event_21" width="371" height="200" class="alignnone size-full wp-image-666" /></p>
<p>在程序上添加自动带出物料描述：</p>
<p><img src="http://www.sapjx.com/wp-content/uploads/2014/11/table_maintenance_view_02_12_Demo_Event_21.jpg" alt="table_maintenance_view_02_12_Demo_Event_21" width="661" height="489" class="alignnone size-full wp-image-668" /></p>
<p>这样在编辑状态时，会自动带出物料描述；</p>
<p>另外，客户编码是该表的主键，不可编辑，因此只需要带出物料描述。</p>
